package service

import (
	"bufio"
	"fmt"
	"github.com/urfave/cli/v2"
	"hcy-api/lib/utils"
	"os"
	"strings"
)

func Do(Version, BuildTime string) (app *cli.App) {
	app = cli.NewApp()
	app.Name = "vap_host_plugin"
	app.Version = fmt.Sprintf("%s build:%s", Version, BuildTime)
	app.Usage = "为 vap host service 提供数据支持的插件"
	app.Description = "可以提供的功能包括：获取主机基础注册信息、获取特定路径下的快捷方式指向的应用信息、主机实时负载信息，创建windows 账户等"
	app.Flags = []cli.Flag{
		&cli.BoolFlag{
			Name:        "info",
			Aliases:     []string{"i"},
			Usage:       "获取主机基础注册信息",
			Value:       false,
			DefaultText: "false",
			Action: func(ctx *cli.Context, b bool) error {
				hp := new(HostPlugin)
				info, err := hp.getRegisterInfo()
				if err != nil {
					return cli.Exit(fmt.Sprintf("获取主机基础注册信息时发送错误:%s", err.Error()), 1)
				}
				w := bufio.NewWriter(os.Stdout)
				_, _ = w.WriteString(string(utils.Obj2Str(info)) + "\n")
				_ = w.Flush()
				return nil
			},
		},
		&cli.StringFlag{
			Name:    "apps",
			Aliases: []string{"a"},
			Usage:   "获取特定路径下的应用信息,请指定快捷方式所在的目录",
			Action: func(ctx *cli.Context, s string) error {
				if s == "" {
					return cli.Exit(fmt.Sprintf("路径不能为空"), 1)
				}
				hp := new(HostPlugin)
				apps, err := hp.getAppList(s)
				if err != nil {
					return cli.Exit(fmt.Sprintf("获取云应用信息时发生错误：%s", err.Error()), 1)
				}
				w := bufio.NewWriter(os.Stdout)
				_, _ = w.WriteString(string(utils.Obj2Str(apps)) + "\n")
				_ = w.Flush()
				return nil
			},
		},
	}
	userCommand := &cli.Command{
		Name:  "user",
		Usage: "操作windows账户，例如创建 windows账户、将用户添加到远程桌面用户组",
		Flags: []cli.Flag{
			&cli.StringFlag{
				Name:    "addRemote",
				Aliases: []string{"ar"},
				Usage:   "创建账户，并将账户添加到远程桌面账户组,格式为：-ar=account-pws,账户密码中都不能使用 '-' 符号 ",
				Action: func(ctx *cli.Context, in string) error {
					ins := strings.Split(in, "-")
					if len(ins) != 2 {
						return cli.Exit(fmt.Sprintf("当前参数：%s,需要接收的参数格式为：account-pws,且密码中不能使用'-'符号", in), 1)
					}
					ph := new(HostPlugin)
					err := ph.createRemoteWinAccount(ins[0], ins[1])
					if err != nil {
						return cli.Exit(fmt.Sprintf("创建远程桌面账户时发生错误：%s", err.Error()), 1)
					}
					return nil
				},
			},
			&cli.IntFlag{
				Name:    "logoffSession",
				Aliases: []string{"ls"},
				Usage:   "注销会话,格式为：-ls=win会话ID",
				Action: func(ctx *cli.Context, id int) error {
					if id == 0 {
						return cli.Exit(fmt.Sprintf("不能注销0号会话"), 1)
					}
					ph := new(HostPlugin)
					err := ph.logoffSession(fmt.Sprintf("%d", id))
					if err != nil {
						return cli.Exit(fmt.Sprintf("注销会话[%d]失败", id), 1)
					}
					return nil
				},
			},
		},
	}
	userCommand.Action = func(ctx *cli.Context) error {
		return nil
	}

	diskCommand := &cli.Command{
		Name:  "disk",
		Usage: "操作用户磁盘、挂载网盘、文件夹权限的操作",
		Flags: []cli.Flag{
			&cli.StringFlag{
				Name:    "mappingDir",
				Aliases: []string{"md"},
				Usage:   "映射目录为盘符，格式为：-md=目录路径-盘符名称.",
				Action: func(ctx *cli.Context, in string) error {
					ins := strings.Split(in, "-")
					if len(ins) != 2 {
						return cli.Exit(fmt.Sprintf("请按格式输入目录路径和盘符"), 1)
					}
					ph := new(HostPlugin)
					err := ph.mappingDir(ins[0], ins[1])
					if err != nil {
						return cli.Exit(fmt.Sprintf("将目录映射为盘符时发生错误：%s", err), 1)
					}
					return nil
				},
			},
			&cli.StringFlag{
				Name:    "delMapDir",
				Aliases: []string{"dd"},
				Usage:   "删除盘符映射,格式为： -dd=Y ,但是不能删除C盘.",
				Action: func(ctx *cli.Context, in string) error {
					if in == "C" {
						return cli.Exit(fmt.Sprintf("该程序不能删除C盘映射"), 1)
					}
					ph := new(HostPlugin)
					err := ph.unmapDrive(in)
					if err != nil {
						return cli.Exit(fmt.Sprintf("删除盘符映射时发生错误：%s", err), 1)
					}
					return nil
				},
			},
			&cli.StringFlag{
				Name:    "crateUserDir",
				Aliases: []string{"cd"},
				Usage:   "创建用户独占目录,格式为：-cd=path-account.",
				Action: func(ctx *cli.Context, in string) error {
					ins := strings.Split(in, "-")
					if len(ins) != 2 {
						return cli.Exit(fmt.Sprintf("请按格式输入目录路径和账户"), 1)
					}
					ph := new(HostPlugin)
					err := ph.createMonopolizeDir(ins[0], ins[1])
					if err != nil {
						return cli.Exit(fmt.Sprintf("创建独占目录时发生错误：%s", err), 1)
					}
					return nil
				},
			},
		},
	}
	diskCommand.Action = func(ctx *cli.Context) error {
		return nil
	}
	app.Commands = cli.Commands{
		userCommand,
		diskCommand,
	}
	app.Action = func(ctx *cli.Context) error {
		return nil
	}
	return app
}
